<?php

class ShopModel extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 新增||修改门店
     * @param Array $data
     */
    public function initShop($data) {
        if ($data['id'] > 0) {
            // 修改
            $this->db->where('id', $data['id']);
            $this->db->update('wechat_shop', $data);
        } else {
            // 新增
            $data['shop_create_time'] = time();
            $data['shop_auth'] = time();
            $count = $this->db->count_all_results('wechat_shop');
            $data['shop_auth'] = mt_rand(100, 10000) . $count;
            $this->db->insert('wechat_shop', $data);
        }
    }

    /**
     * 根据公众号ID获取门对应门店
     * 数据总条数
     * @param type $public_id
     * @return INT 总条数
     */
    public function getShopCountById($public_id) {
        $this->db->where('shop_public_id', $public_id);
        $count = $this->db->count_all_results('wechat_shop');
        return $count;
    }

    /**
     * 根据公众号ID获取门对应门店
     * 总数据
     * @param type $public_id
     * @return type
     */
    public function getShopListById($public_id) {
        $this->db->where('shop_public_id', $public_id);
        $query = $this->db->get('wechat_shop');
        return $query->result_array();
    }

    /**
     * 根据门店ID
     * 查询门店信息
     * @param type $shop_id
     * @return type
     */
    public function getShopDetailById($shop_id) {
        $query = $this->db->get_where('wechat_shop', array('id' => $shop_id, 'shop_admin_id' => $this->session->userdata('uid')));
        return $query->row_array();
    }


}
